package com.xyz.admin.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.sc.base.admin.config.KaptchaConfig;
import com.sc.base.admin.config.LongToStringJsonConfig;
import com.sc.base.admin.config.ScBaseAdminConfiguration;
import com.sc.base.common.spring.config.MybatisSQLPerConfiguration;
import com.sc.base.common.spring.config.RedisConfiguration;
import com.sc.base.common.spring.config.ShardingDataSourceConfiguration;
import com.sc.base.common.spring.security.SpringSecurityBaseConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.*;
import org.springframework.core.annotation.Order;

import java.util.concurrent.*;

@Configuration
@MapperScan(basePackages = {"com.xyz.admin.**.mapper"})
@ComponentScan({"com.xyz.admin.**.service"})
@Import({MybatisSQLPerConfiguration.class, ShardingDataSourceConfiguration.class, RedisConfiguration.class,
        SpringSecurityBaseConfiguration.class,ScBaseAdminConfiguration.class,Swagger2Config.class,HazelcastConfiguration.class})
public class ByteWorldConfiguration {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Bean(value = "threadPool")
    @Primary
    public ExecutorService buildThreadPool() {
        int cpuNum = Runtime.getRuntime().availableProcessors();
        logger.info("init buildThreadPool,cpuNum:{}....",cpuNum);
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-pool-%d").build();
        int corePoolSize = cpuNum;
        int maxPoolSize = cpuNum * 2;
        long keepAliveTime = 10000L;
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
        RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
        return new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime,
                TimeUnit.MILLISECONDS, workQueue, namedThreadFactory, rejectedExecutionHandler);
    }

}
